package com.epalmpay.service.mall.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.epalmpay.util.Base64Utils;
import jodd.util.URLDecoder;
import org.springframework.stereotype.Service;

import com.epalmpay.dto.mall.MallGoodCommentInDTO;
import com.epalmpay.dto.mall.MallGoodCommentOutDTO;
import com.epalmpay.entity.MallGoodComment;
import com.epalmpay.entity.Page;
import com.epalmpay.mapper.MallGoodCommentMapper;
import com.epalmpay.service.mall.IMallGoodCommentService;
@Service("mallGoodCommentService")
public class MallGoodCommentServiceImpl implements IMallGoodCommentService{
	
	@Resource 
	private MallGoodCommentMapper mallGoodCommentMapper;
	
	@Override
	public List<MallGoodCommentOutDTO> MallGoodCommentList(MallGoodCommentInDTO mallGoodCommentInDTO, Page page)
			throws Exception {
		
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("commentLevel", mallGoodCommentInDTO.getCommentLevel());
		param.put("merchantName",mallGoodCommentInDTO.getMerchantName());
		param.put("auditStatus", mallGoodCommentInDTO.getAuditStatus());
		param.put("joinStart", mallGoodCommentInDTO.getJoinStart());
		param.put("joinEnd", mallGoodCommentInDTO.getJoinEnd());
		param.put("groupId", mallGoodCommentInDTO.getGroupId());
		param.put("commentType", mallGoodCommentInDTO.getCommentType());


		
		List<MallGoodCommentOutDTO> result = new ArrayList<MallGoodCommentOutDTO>();

		int sumCount = mallGoodCommentMapper.selectMallGoodCommentListCount(param);
		page.setTotalResult(sumCount);
		if(sumCount<=0)
			return null;
		
		param.put("limit", page.getShowCount());
		param.put("start", page.getShowCount()*(page.getCurrentPage()-1));
		
		List<Map<String,Object>> mallGoodCommentList = mallGoodCommentMapper.selectMallGoodCommentList(param);
		
		for (Map<String, Object> mallGoodComment : mallGoodCommentList) {
			MallGoodCommentOutDTO mallGoodCommentOutDTO = new MallGoodCommentOutDTO();
			
			mallGoodCommentOutDTO.setId((Long)(mallGoodComment.get("id")));

			if(Integer.parseInt(String.valueOf(mallGoodComment.get("comment_type")))==1){
				mallGoodCommentOutDTO.setContent((String)mallGoodComment.get("content"));
			}else{//云流消费订单评价
				String content = (String) mallGoodComment.get("content");
				String decode =Base64Utils.getFromBase64(content);
				mallGoodCommentOutDTO.setContent(decode);
			}

//			mallGoodCommentOutDTO.setContent((String)mallGoodComment.get("content"));



			mallGoodCommentOutDTO.setCommentLevel((Integer)mallGoodComment.get("comment_level"));
			mallGoodCommentOutDTO.setPicUrls((String)mallGoodComment.get("pic_urls"));
			mallGoodCommentOutDTO.setJoinDate((Date)mallGoodComment.get("gmt_create"));
			mallGoodCommentOutDTO.setMemberName((String)mallGoodComment.get("member_name"));
			mallGoodCommentOutDTO.setMemberGradeName((String)mallGoodComment.get("member_grade_name"));
			mallGoodCommentOutDTO.setGoodName((String)mallGoodComment.get("good_name"));
			mallGoodCommentOutDTO.setMerchantName((String)mallGoodComment.get("merchant_name"));//门店名称
			mallGoodCommentOutDTO.setPromotionPrice((Integer)mallGoodComment.get("promotion_price"));
			mallGoodCommentOutDTO.setAuditStatus((Integer)mallGoodComment.get("audit_status"));
			result.add(mallGoodCommentOutDTO);
		}
		
		return result;
	}
	
	//编辑时 根据id 查询信息回显
	@Override
	public MallGoodComment findMallGoodCommentById(MallGoodComment mallGoodComment) {
		return mallGoodCommentMapper.findMallGoodCommentById(mallGoodComment.getId());
	}


	@Override
	public void updateMallGoodComment(MallGoodComment mallGoodComments) {
//		mallGoodCommentMapper.updateByPrimaryKeySelective(mallGoodComments);
		mallGoodCommentMapper.updateByPrimaryKeySelective(mallGoodComments);

	}


	@Override
	public MallGoodComment findMallGoodComment(Long id) {
	
		return mallGoodCommentMapper.findMallGoodComment(id);
	}
	
	@Override
	public void updateStatus(MallGoodComment mallGoodComments) {
		if (mallGoodComments.getAuditStatus()==0) {
			mallGoodComments.setAuditStatus(1);
		}
		else{
			mallGoodComments.setAuditStatus(0);
		}
		mallGoodCommentMapper.updateByPrimaryKeySelective(mallGoodComments);
	}

	

	 /**
    * 根据当前选中项,更新状态,然后标记更新成功标志或者失败标志,存放列表
    * @param mallGoodCommentInDTOList
    * @return
    */

	@Override
	public List<Integer> updateAuditStatus(List<MallGoodCommentInDTO> mallGoodCommentInDTOList) {
		 List<Integer> list = new ArrayList<Integer>();


	        for (int i = 0; i<mallGoodCommentInDTOList.size(); i++){
	        	MallGoodComment mallGoodComment = new MallGoodComment();
	        	mallGoodComment.setGroupId(mallGoodCommentInDTOList.get(i).getGroupId());
	        	mallGoodComment.setId(mallGoodCommentInDTOList.get(i).getId());
	        	mallGoodComment.setAuditStatus(mallGoodCommentInDTOList.get(i).getAuditStatus());
	            list.add(mallGoodCommentMapper.updateByPrimaryKeySelective(mallGoodComment));
	        }

	        return list;
	}
	

	@Override
	public MallGoodComment findSheZhiById(MallGoodComment mallGoodComment) {
		return mallGoodCommentMapper.findSheZhiById(mallGoodComment.getGroupId());
	}

	
	/**
	 * 门店后台的评价管理
	 */

	@Override
	public List<MallGoodCommentOutDTO> MallGoodCommentShopList(MallGoodCommentInDTO mallGoodCommentInDTO, Page page)
			throws Exception {
		
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("commentLevel", mallGoodCommentInDTO.getCommentLevel());
		param.put("auditStatus", mallGoodCommentInDTO.getAuditStatus());
		param.put("joinStart", mallGoodCommentInDTO.getJoinStart());
		param.put("joinEnd", mallGoodCommentInDTO.getJoinEnd());
		param.put("shopId", mallGoodCommentInDTO.getShopId());
		param.put("commentType", mallGoodCommentInDTO.getCommentType());

		
		
		List<MallGoodCommentOutDTO> result = new ArrayList<MallGoodCommentOutDTO>();

		int sumCount = mallGoodCommentMapper.queryMallGoodCommentListCount(param);
		page.setTotalResult(sumCount);
		if(sumCount<=0)
			return null;
		
		param.put("limit", page.getShowCount());
		param.put("start", page.getShowCount()*(page.getCurrentPage()-1));
		
		List<Map<String,Object>> mallGoodCommentList = mallGoodCommentMapper.queryMallGoodCommentList(param);
		
		for (Map<String, Object> mallGoodComment : mallGoodCommentList) {
			MallGoodCommentOutDTO mallGoodCommentOutDTO = new MallGoodCommentOutDTO();
			
			mallGoodCommentOutDTO.setId((Long)(mallGoodComment.get("id")));
//

			if(Integer.parseInt(String.valueOf(mallGoodComment.get("comment_type")))==1){
				mallGoodCommentOutDTO.setContent((String)mallGoodComment.get("content"));
			}else{//云流消费订单评价
				String content = (String) mallGoodComment.get("content");
				String decode =Base64Utils.getFromBase64(content);
				mallGoodCommentOutDTO.setContent(decode);
			}


			mallGoodCommentOutDTO.setCommentLevel((Integer)mallGoodComment.get("comment_level"));
			mallGoodCommentOutDTO.setPicUrls((String)mallGoodComment.get("pic_urls"));
			mallGoodCommentOutDTO.setJoinDate((Date)mallGoodComment.get("gmt_create"));
			mallGoodCommentOutDTO.setMemberName((String)mallGoodComment.get("member_name"));
			mallGoodCommentOutDTO.setMemberGradeName((String)mallGoodComment.get("member_grade_name"));
			mallGoodCommentOutDTO.setGoodName((String)mallGoodComment.get("good_name"));
			mallGoodCommentOutDTO.setMerchantName((String)mallGoodComment.get("merchant_name"));//门店名称
			mallGoodCommentOutDTO.setPrice((Integer)mallGoodComment.get("price"));
			mallGoodCommentOutDTO.setAuditStatus((Integer)mallGoodComment.get("audit_status"));
			result.add(mallGoodCommentOutDTO);
		}
		
		return result;
	}



	/**
	 * 编辑时更新图片
	 */
//	@Override
//	public void updateMallGoodCommentShopPicure(MallGoodComment mallGoodComment) {
//		mallGoodCommentMapper.updateByPrimaryKeySelective(mallGoodComment);
//	}
//	



}
